package d4_safe2;

import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

public class Account {
    private double money;
    private String CardId;
    //创建锁对象
    private final Lock lock = new ReentrantLock();

    public String getCardId() {
        return CardId;
    }

    public void setCardId(String cardId) {
        CardId = cardId;
    }

    public Account(double money, String cardId) {
        this.money = money;
        CardId = cardId;
    }

    public Account() {
    }


    public void drawMoney(double money) {
            String name = Thread.currentThread().getName();
            lock.lock();
        try {
            if(this.money>=money){
                System.out.println(name+"取钱成功");
                this.money-=money;
                System.out.println("余额为"+this.money);
            }else{
                System.out.println("余额不足");
            }
        } finally {
            lock.unlock();
        }
    }
}
